<!--车辆管理页-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">车辆管理</span>
    <el-button @click="beforeAddVehicle" type="primary" style="float:right;margin-top:13px;">新建车辆</el-button>
  </div>
  <!-- 新增车辆弹窗 -->
  <el-dialog :title="dialogTitle" v-model="dialogVisible" style="width:1000px;padding:40px;" :before-close="handleClose">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车辆品牌">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.brand"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆牌号">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.license"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车辆型号">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.model"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆识别码">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.code"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车辆排量">
            <el-select placeholder="请选择" v-model="saveVehicleForm.displacement">
              <el-option v-for="item in vehicleDisplacementArr" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆类型">
            <el-select placeholder="请选择" v-model="saveVehicleForm.type">
              <el-option v-for="item in vehicleTypeArr" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车身颜色">
            <el-select placeholder="请选择" v-model="saveVehicleForm.color">
              <el-option v-for="item in vehicleColorArr" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="里程数">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.kilometers"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="购买时间">
            <el-date-picker type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD" v-model="saveVehicleForm.buyTime" @change="validateDate"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="上牌时间">
            <el-date-picker type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD" v-model="saveVehicleForm.regTime" @change="validateDate"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="购买价格">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.price"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电池类型">
            <el-select placeholder="请选择" v-model="saveVehicleForm.batteryType">
              <el-option v-for="item in batteryTypeArr" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveVehicle">确定</el-button>
    </template>
  </el-dialog>
  <!-- 车辆搜索卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="车辆品牌">
        <el-input placeholder="请输入内容" style="width:220px;" v-model="searchVehicleForm.brand" @keydown.enter="loadVehicle"></el-input>
      </el-form-item>
      <el-form-item label="车牌号" style="width:290px;">
        <el-input placeholder="请输入内容" style="width:220px;" v-model="searchVehicleForm.license" @keydown.enter="loadVehicle"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadVehicle">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 车辆列表 -->
  <el-card style="margin:20px;">
    <el-table :data="vehicleArr">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="brand" label="车辆品牌" align="center"/>
      <el-table-column prop="license" label="车牌号" align="center"/>
      <el-table-column prop="code" label="车辆识别码" align="center"/>
      <el-table-column prop="type" label="车辆类型" align="center" :formatter="vehicleTypeFormatter"/>
      <el-table-column prop="price" label="购买价格" align="center"/>
      <el-table-column prop="buyTime" label="购买时间" align="center"/>
      <el-table-column prop="regTime" label="上牌时间" align="center"/>
      <el-table-column prop="batteryType" label="电池类型" align="center" :formatter="batteryTypeFormatter"/>
      <el-table-column prop="status" label="车辆状态" align="center" :formatter="vehicleStatusFormatter"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="editVehicle(scope.row.id)">编辑</el-button>
          <el-button link type="primary" size="small" @click="deleteVehicle(scope.row.id) ">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>
<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import qs from "qs";
import {ElMessage} from "element-plus";


//控制新增车辆弹窗是否显示
const dialogVisible = ref(false);
//定义变量控制dialog的标题
const dialogTitle = ref('新增车辆');
const searchVehicleForm = ref({brand:'',license:''})
const vehicleArr = ref([]);
//定义数组用来保存车辆排量字典项
const vehicleDisplacementArr = ref([]);
//定义数组用来保存车辆类型字典项
const vehicleTypeArr = ref([]);
//定义数组用来保存车身颜色字典项
const vehicleColorArr = ref([]);
//定义数组用来保存电池类型字典项
const batteryTypeArr = ref([]);
//定义数组用来保存车辆状态字典项
const vehicleStatusArr = ref([]);
onMounted(()=>{
  loadDictOptions(vehicleDisplacementArr,'vehicle_displacement');
  loadDictOptions(vehicleTypeArr,'vehicle_type');
  loadDictOptions(vehicleColorArr,'vehicle_color');
  loadDictOptions(batteryTypeArr,'battery_type');
  loadDictOptions(vehicleStatusArr,'vehicle_status');
  console.log(vehicleArr);
})
const loadVehicle = () => {
  console.log(searchVehicleForm.value);
  let data = qs.stringify(searchVehicleForm.value);
  axios.get(BASE_URL+'/v1/vehicle/select?'+data)
      .then((response)=>{
        if(response.data.code==2000){
          vehicleArr.value = response.data.data;
        }else{
          ElMessage.error(response.data.msg);
        }
      })
}
onMounted (() =>{
  loadVehicle();
})
const resetSearch = () => {
  searchVehicleForm.value={};
  loadVehicle();
}
const vehicleTypeFormatter = (row,column,cellValue,index) => {
  //如果字典项数组没有值,直接返回当前单元格的值,比如 10
  if(!vehicleTypeArr.value){
    return cellValue;
  }
  //依次遍历出字典项数组中的每个字典项
  for(let item of vehicleTypeArr.value){
    //如果字典项的value与当前单元格的值一致
    if(item.value==cellValue){
      //将字典项的label赋值给当前单元格, 比如 轿车
      cellValue = item.label;
    }
  }
  return cellValue;
};
const batteryTypeFormatter = (row,column,cellValue,index) => {
  //如果字典项数组没有值,直接返回当前单元格的值,比如 10
  if(!batteryTypeArr.value){
    return cellValue;
  }
  //依次遍历出字典项数组中的每个字典项
  for(let item of batteryTypeArr.value){
    //如果字典项的value与当前单元格的值一致
    if(item.value==cellValue){
      //将字典项的label赋值给当前单元格, 比如 轿车
      cellValue = item.label;
    }
  }
  return cellValue;
};
const vehicleStatusFormatter = (row,column,cellValue,index) => {
  //如果字典项数组没有值,直接返回当前单元格的值,比如 10
  if(!vehicleStatusArr.value){
    return cellValue;
  }
  //依次遍历出字典项数组中的每个字典项
  for(let item of vehicleStatusArr.value){
    //如果字典项的value与当前单元格的值一致
    if(item.value==cellValue){
      //将字典项的label赋值给当前单元格, 比如 轿车
      cellValue = item.label;
    }
  }
  return cellValue;
}
//定义对象用来收集车辆弹窗表单的所有数据
const saveVehicleForm = ref({
  brand:'',
  license:'',
  model:'',
  code:'',
  displacement:'',
  type:'',
  color:'',
  kilometers:'',
  buyTime:'',
  regTime:'',
  price:'',
  batteryType:'',
})
const validateDate = () => {
  const buyTime = saveVehicleForm.value.buyTime;
  const regTime = saveVehicleForm.value.regTime;
  if(regTime && buyTime && new Date(regTime) <= new Date(buyTime)){
    ElMessage.error('上牌时间不能早于购买时间!');
    saveVehicleForm.value.regTime = '';
  }

}
const saveVehicle = () => {
  console.log(saveVehicleForm.value);
  //对数据进行校验
  if(!saveVehicleForm.value.license || saveVehicleForm.value.license.trim()==''){
    ElMessage.error('请输入车牌号!');
    return;
  }
  if(!saveVehicleForm.value.code || saveVehicleForm.value.code.trim()==''){
    ElMessage.error('请输入车辆识别码!');
    return;
  }
  if(!saveVehicleForm.value.regTime){
    ElMessage.error('请输入上牌时间!');
    return;
  }
  if(!saveVehicleForm.value.buyTime){
    ElMessage.error('请输入构造时间!');
    return;
  }
  let data = qs.stringify(saveVehicleForm.value);
  axios.post(BASE_URL+'/v1/vehicle/save',data)
      .then((response)=>{
        if(response.data.code == 2000){
          ElMessage.success('保存成功!');
          dialogVisible.value = false;
          saveVehicleForm.value={};
          loadVehicle();
        }else{
          //数据库里的code和license字段设置了唯一约束,如果重复插入会操作失败,所以可以设置更确切的提示语句
          ElMessage.error(response.data.msg);
        }
      })
}
const handleClose = () => {
  if(confirm('您确认要关闭本弹窗吗?')){
    dialogVisible.value = false;
  }
}
const beforeAddVehicle = () => {
  dialogVisible.value = true;
  dialogTitle.value = '新增车辆';
  saveVehicleForm.value={};
}
const editVehicle = (id) => {
  dialogVisible.value = true;
  dialogTitle.value = '编辑车辆';
  axios.get(BASE_URL+'/v1/vehicle/select?id='+id)
      .then((response)=>{
        if(response.data.code == 2000){
          saveVehicleForm.value = response.data.data[0];
        }else{
          ElMessage.error(response.data.msg);
        }
      })

}
const deleteVehicle = (id) => {
  if(confirm('您确认要删除本条记录吗？')){
    axios.post(BASE_URL+'/v1/vehicle/delete/'+id)
        .then((response)=>{
          if(response.data.code==2000){
            ElMessage.success('删除成功!');
            loadVehicle();
          }else{
            ElMessage.error(response.data.msg);
          }
        })
  }

}
</script>